<template>
  <div>
    <template
      v-if="
        hasOneShowingChild(item.children, item) &&
        (!onlyOneChild.children || onlyOneChild.noShowingChildren) &&
        !item.alwaysShow
      "
    >
      <el-menu-item v-if="onlyOneChild.outside" :index="onlyOneChild.path">
        <a class="outside" style="display: block;"  target="_blank" :href="onlyOneChild.meta.link">{{onlyOneChild.meta.title}}</a>
      </el-menu-item>

      <app-link v-else-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
        <!--<el-menu-item :index="'/'+onlyOneChild.name" :class="{ 'submenu-title-noDropdown': !isNest }" @click="fn(onlyOneChild)">-->
        <!--<el-menu-item :index="basePath" :class="{ 'submenu-title-noDropdown': !isNest }" @click="fn(onlyOneChild)">-->
        <el-menu-item
          :index="findTreeId(router, onlyOneChild.id)"
          :class="{ 'submenu-title-noDropdown': !isNest }"
          @click="fn(onlyOneChild)"
        >
          <item
            :icon="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"
            :title="onlyOneChild.meta.title"
          />
        </el-menu-item>
      </app-link>
    </template>
    <!--主题菜单-->
    <el-submenu
      v-else
      ref="subMenu"
      :index="resolvePath(item.path)"
      popper-append-to-body
    >
      <template slot="title">
        <item
          v-if="item.meta"
          :icon="item.meta && item.meta.icon"
          :title="item.meta.title"
        />
      </template>
      <menus-item
        v-for="child in item.children"
        :key="child.id"
        :is-nest="true"
        :item="child"
        :base-path="resolvePath(child.path)"
        class="nest-menu"
      />
    </el-submenu>
  </div>

  <!--<el-menu-item v-if="item.children&&item.children.length < 1" :index="'/'+item.name" @click="fn(pathRou())">
		<span class="menuSty"><b></b>
			{{this.item.meta.title}}
		</span>
	</el-menu-item>
	<el-submenu :index="basePath" v-else>
        <template slot="title">
        	<b class="_b"></b>
			{{this.item.meta.title}}
        </template>
          <el-menu-item class="_child" v-for="item,index in item.children" :index="resolvePath(item.path)"
          	@click="fn(resolvePath(item.path))"
          	:key="resolvePath(item.path)">
          	<b class="_b"></b>
          	{{item.meta.title}}
          </el-menu-item>
     </el-submenu>-->
</template>

<script>
import { isExternal } from "@/utils/validate";
import path from "path";
import Item from "./Item";
import { mapState } from "vuex";
import AppLink from "./Link";
export default {
  name: "MenusItem", //组件自己调用自己
  components: { Item, AppLink },
  computed: {
    ...mapState(["router"]),
  },
  props: {
    item: {
      type: Object,
      required: true,
    },
    isNest: {
      type: Boolean,
      default: false,
    },
    basePath: {
      type: String,
      default: "",
    },
  },
  created() {
    //		console.log(this.item)
  },
  data() {
    this.onlyOneChild = null;
    return {};
  },
  methods: {
    fn(data) {
      //			console.log(this.basePath)
      //			console.log(data.name)
    },
    pathRou(routePath) {
      return this.basePath;
    },
    hasOneShowingChild(children = [], parent) {
      const showingChildren = children.filter((item) => {
        if (item.hidden) {
          return false;
        } else {
          this.onlyOneChild = item;
          return true;
        }
      });

      if (showingChildren.length === 1) {
        return true;
      }
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
        return true;
      }

      return false;
    },
    resolvePath(routePath) {
      if (isExternal(routePath)) {
        return routePath;
      }
      if (isExternal(this.basePath)) {
        return this.basePath;
      }
      return path.resolve(this.basePath, routePath);
    },
    //根据当前id 追述拼接全部path
    findTreeId(array, id) {
      let stack = [];
      let going = true;
      let walker = (array, id) => {
        array.forEach((item) => {
          if (!going) return;
          stack.push(item["path"]);
          if (item["id"] === id) {
            going = false;
          } else if (item["children"]) {
            walker(item["children"], id);
          } else {
            stack.pop();
          }
        });
        if (going) stack.pop();
      };
      walker(array, id);
      if (stack.length && stack) {
        return stack.join("/");
      } else {
        return false;
      }
    },
  },
};
</script>
<style>
.el-submenu{
  width: 230px;
  background: #1A2A37!important;
}
.el-submenu__title {
  color: #fff !important;
  background: #1A2A37!important;
  /*box-sizing: border-box;padding-left: 32px !important;*/
}
.el-menu-item {
  background: #1A2A37!important;
  color: #fff !important;
}
._b {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 9px;
  position: relative;
  border-radius: 50%;
  background: #acb8b8;
}
._child {
  box-sizing: border-box;
  padding-left: 64px !important;
}
.el-menu-item.is-active .outside{
  color: #006080 !important;
}
</style>
